package com.raycloud.robot.utils;

import java.time.temporal.TemporalAccessor;
import java.time.format.DateTimeFormatter;
import com.raycloud.robot.domain.dto.KeyValue;
import java.time.LocalTime;
import java.time.chrono.ChronoLocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.util.Objects;
import com.raycloud.robot.enums.EnumApplyTimeType;
import java.time.ZoneId;
import org.apache.commons.lang3.StringUtils;
import com.raycloud.robot.domain.dto.ApplyTimeDto;
import java.time.LocalDateTime;

public class ApplyTimeUtil{
    private static final LocalDateTime minTime;
    private static final LocalDateTime maxTime;
    public static Boolean applyTimeCheck(final ApplyTimeDto applyTime,final Long currentTime){
        final Integer applyTimeId=applyTime.getApplyTimeId();
        if(1==applyTimeId){
            return Boolean.TRUE;
        }
        if(2==applyTimeId){
            return Boolean.FALSE;
        }
        final String dateTime=applyTime.getTime();
        LocalDateTime now=LocalDateTime.now();
        if(currentTime!=null){
            now=DateUtil.longToLocalDateTime(currentTime);
        }
        final LocalTime localTime=now.toLocalTime();
        String startTime;
        String endTime;
        if(StringUtils.isBlank(dateTime)){
            startTime=String.valueOf(applyTime.getStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
            endTime=String.valueOf(applyTime.getEndTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        }
        else{
            startTime=dateTime.split("-")[0];
            endTime=dateTime.split("-")[1];
        }
        if(Objects.equals(applyTime.getType(),EnumApplyTimeType.FIX_TIME.getEnName())){
            final LocalDateTime start=Instant.ofEpochMilli(Long.parseLong(startTime)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            final LocalDateTime end=Instant.ofEpochMilli(Long.parseLong(endTime)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            if(now.isAfter(start)&&now.isBefore(end)){
                return Boolean.TRUE;
            }
        }
        if(Objects.equals(applyTime.getType(),EnumApplyTimeType.CYCLE_TIME_DAY.getEnName())){
            final LocalTime start2=LocalTime.parse(startTime);
            final LocalTime end2=LocalTime.parse(endTime);
            final boolean before=start2.isBefore(end2);
            if(before){
                if(localTime.isAfter(start2)&&localTime.isBefore(end2)){
                    return Boolean.TRUE;
                }
            }
            else if(localTime.isAfter(start2)||localTime.isBefore(end2)){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
    public static KeyValue<String,String> getApplyTime(final ApplyTimeDto applyTimeDto,final String pattern){
        final KeyValue<String,String> keyValue=new KeyValue<String,String>();
        LocalDateTime startTime;
        LocalDateTime endTime;
        if(StringUtils.isBlank(applyTimeDto.getTime())||applyTimeDto.getApplyTimeId()==1){
            if(applyTimeDto.getApplyTimeId()==1){
                startTime=ApplyTimeUtil.minTime;
                endTime=ApplyTimeUtil.maxTime;
            }
            else{
                startTime=applyTimeDto.getStartTime();
                endTime=applyTimeDto.getEndTime();
            }
        }
        else{
            final String start=applyTimeDto.getTime().split("-")[0];
            final String end=applyTimeDto.getTime().split("-")[1];
            if(!Objects.equals(applyTimeDto.getType(),EnumApplyTimeType.FIX_TIME.getEnName())){
                keyValue.setKey(start);
                keyValue.setValue(end);
                return keyValue;
            }
            startTime=Instant.ofEpochMilli(Long.parseLong(start)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            endTime=Instant.ofEpochMilli(Long.parseLong(end)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        }
        if(StringUtils.isNotBlank(pattern)){
            final DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern);
            keyValue.setKey(formatter.format(startTime));
            keyValue.setValue(formatter.format(endTime));
        }
        else{
            keyValue.setKey(startTime.toString());
            keyValue.setValue(endTime.toString());
        }
        return keyValue;
    }
    static{
        minTime=LocalDateTime.of(2021,1,1,0,0,0);
        maxTime=LocalDateTime.of(2030,12,30,23,59,59);
    }
}
